﻿using ChineseSchool.BLL;
using ChineseSchool.Models.Hethong;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace ChineseSchool.Web
{
    public partial class UserInfo : System.Web.UI.Page
    {
        B_User userService = new B_User();
        B_Insert_Update_Delete insertUpdate = new B_Insert_Update_Delete();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                GetUserInfo();
            }
        }

        private void GetUserInfo()
        {
            string userName = Session["Username"].ToString();
            string userPassword = Session["PassWord"].ToString();
            var currentUser = userService.Selectby_username_password(userName, userPassword);

            if (currentUser != null)
            {
                if (!string.IsNullOrEmpty(currentUser.Avatar))
                    imgAvatar.ImageUrl = "/FilesUpload/" + currentUser.Avatar;
                else
                    imgAvatar.ImageUrl = "/images/noavatar.png";

                txtUserName.Text = currentUser.UserName;
                txtFullName.Text = currentUser.FullName;
                txtBirthday.Text = currentUser.Birthday.ToString();
                txtEmail.Text = currentUser.Email;
                txtPhone.Text = currentUser.PhoneNumber;
                txtAddress.Text = currentUser.Address;

                if (!string.IsNullOrEmpty(currentUser.Gender))
                    ddlGender.Items.FindByValue(currentUser.Gender).Selected = true;
            }
        }

        private bool CheckOldPass()
        {
            if (txtOldPass.Text.Trim().Equals(Session["PassWord"].ToString()))
                return true;
            return false;
        }

        protected void BtnUpdateClicked(object sender, EventArgs e)
        {
            string userName = Session["Username"].ToString();
            string userPassword = Session["PassWord"].ToString();
            var currentUser = userService.Selectby_username_password(userName, userPassword);

            if (!string.IsNullOrEmpty(txtOldPass.Text.Trim()))
            {
                if (!CheckOldPass())
                {
                    Response.Write("<script type='text/javascript'>alert('Mật khẩu cũ không đúng!')</script>");
                    return;
                }
            }

            if (!string.IsNullOrEmpty(txtPassword.Text.Trim()) || !string.IsNullOrEmpty(txtRePass.Text.Trim()))
            {
                if (txtPassword.Text.Trim().Equals(txtRePass.Text.Trim()))
                    currentUser.Password = txtRePass.Text;
                else
                {
                    Response.Write("<script type='text/javascript'>alert('Mật khẩu mới không trùng nhau!')</script>");
                    return;
                }
            }            

            if (!string.IsNullOrEmpty(txtUserName.Text.Trim()))
                currentUser.UserName = txtUserName.Text.Trim();

            if (!string.IsNullOrEmpty(txtFullName.Text.Trim()))
                currentUser.FullName = txtFullName.Text.Trim();

            if (!string.IsNullOrEmpty(txtEmail.Text.Trim()))
                currentUser.Email = txtEmail.Text.Trim();

            if (!string.IsNullOrEmpty(txtPhone.Text.Trim()))
                currentUser.PhoneNumber = txtPhone.Text.Trim();

            if (!string.IsNullOrEmpty(txtAddress.Text.Trim()))
                currentUser.Address = txtAddress.Text.Trim();

            if (!string.IsNullOrEmpty(ddlGender.SelectedItem.Value))
                currentUser.Gender = ddlGender.SelectedItem.Value;

            if (!string.IsNullOrEmpty(txtBirthday.Text.Trim()))
                currentUser.Birthday = Convert.ToDateTime(txtBirthday.Text);

            if (!string.IsNullOrEmpty(imgAvatar.ImageUrl))
            {
                var getAvatarUrl = imgAvatar.ImageUrl.Split('/');
                var fileName = getAvatarUrl[getAvatarUrl.Length - 1];
                currentUser.Avatar = fileName;
            }

            insertUpdate.update("Users", "UserID", currentUser, currentUser.UserID);
            Response.Write("<script type='text/javascript'>alert('Cập nhập thành công')</script>");
        }

        protected void BtnSelectImageClick(object sender, EventArgs e)
        {
            if (uploadAvatar.HasFile)
            {
                var fileName = Session["Username"].ToString() + DateTime.Now.ToString("yyyyMMddHHssfff") + "_" + uploadAvatar.FileName;
                var filePath = Server.MapPath("/FilesUpload/" + fileName);
                uploadAvatar.SaveAs(filePath);
                imgAvatar.ImageUrl = "/FilesUpload/" + fileName;
            }
        }
    }
}